package cn.virens.web.components.open.wxma.config;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;

import cn.hutool.core.convert.Convert;

/**
 * 基于Redis实现微信配置缓存
 * 
 * @author virens
 */
public class WxMaCacheConfigByRedis extends AbstractWxMaCacheConfig implements InitializingBean {
	public static final TimeUnit UNIT = TimeUnit.SECONDS;

	@Autowired
	@Qualifier("redisTemplate")
	private RedisTemplate<String, Object> redisTemplate;

	@Override
	protected int ttl(String key) {
		return Convert.toInt(redisTemplate.getExpire(key, UNIT), 0);
	}

	@Override
	protected void rm(String key) {
		this.redisTemplate.delete(key);
	}

	@Override
	protected String get(String key) {
		return Convert.toStr(redisTemplate.opsForValue().get(key), null);
	}

	@Override
	protected void set(String key, String value, int ttl) {
		redisTemplate.opsForValue().set(key, value, ttl, UNIT);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
	}

}
